package com.kgame5.xtank3d.model.tank.comp {
import gs.TweenLite;
import gs.easing.Bounce;

import kgame5.enginex.EntityVO;
import kgame5.enginex.enum.Layer3DEnum;
import kgame5.enginex.part.GViewComp;
import kgame5.k3d.core.math.Matrix3D;
import kgame5.k3d.core.math.Vector3D;
import kgame5.k3d.core.obj3d.node3d.Node3D;
import kgame5.k3d.core.obj3d.node3d.group3d.FastGroup3D;
import kgame5.k3d.util3d.Effect3D;
import kgame5.k3d.util3d.Shaker;
import kgame5.kgui.ColorManager;
import kgame5.kmath.MathDG;

import com.kgame5.xtank3d.C;
import com.kgame5.xtank3d.effect.Eff3DManager;
import com.kgame5.xtank3d.model.tank.comp.flag.TagPane;
import com.kgame5.xtank3d.model.tank.comp.flag.TankTip;
import com.kgame5.xtank3d.resource.ResourceManagerX;
import com.kgame5.xtank3d.vo.MyTankVO;

/**
 * @author kk
 */
public class MyTankViewComp extends GViewComp{
	
	private var 
		md:FastGroup3D,
			md_tank:TankGroup3D,
			tagPane:TagPane,
			tankTip:TankTip;
			
	
	private var vo:MyTankVO;//这么用不太好，这里只是为了方便使用而已
	
	public function MyTankViewComp(vo:MyTankVO){
		this.vo=vo;
		
		md=new FastGroup3D();
		md.layerID = Layer3DEnum.LAYER_MAIN;//千万要加上这个
		
		//坦克实体
		md_tank=new TankGroup3D(vo.typeID);
		md.addChild(md_tank);
		
		md_tank.md_barb.gotoAndStop("stop");
		
		//影子
		md_shadow=ResourceManagerX.getInstance().getShadow(vo.hardVO.bball.R);
		md_shadow.layerID=Layer3DEnum.LAYER_SHADOW;
		
		
		//面板
		tagPane=new TagPane();
		tagPane.md.y = vo.hardVO.aabb.H/2+20;
		md.addChild(tagPane.md);
		tankTip=new TankTip();
		tankTip.md.y = vo.hardVO.aabb.H/2+20;
		md.addChild(tankTip.md);
		
		tagPane.setHP(vo.hp, vo.body.HP);
		tagPane.setName(vo.name);
		
		//烟尘
		initDusts();
	}

	override public function getNode3D():Node3D{
		return md;
	}
	
	override public function update(_vo:EntityVO):void{
		
		super.update(_vo);//影子
		
		//整体
		md.ti.hardVO.v3d_p.copy(vo.hardVO.v3d_p);//位置
		md.rotY = vo.hardVO.v3d_r.y;//旋转
		
		//炮塔旋转
		md_tank.md_barbG.rotY = vo.barbRotY;
		
		//轮子
		wheelsMove();
	}

	override public function dispose():void{
		
	}
	//----------------------外部命令----------------------
	public function bornStart():void{
		md.ti.hardVO.v3d_s.fill(C.BORN_SCALE, C.BORN_SCALE, C.BORN_SCALE);
		TweenLite.to(md.ti.hardVO.v3d_s, Number(vo.body.bornDurMS/1000), {x:1,y:1,z:1,ease:Bounce.easeOut});
		ColorManager.whiteColor(md.getDO());
	}
	public function bornEnd():void{
		ColorManager.noneColor(md.getDO());
	}
	
	public function hurtStart(hurt:int):void{
		tankTip.addFlyTip(hurt);
		tagPane.setHP(vo.hp, vo.body.HP);
		ColorManager.redColor(md_tank.getDO());
		
		Effect3D.Jelly(md_tank,1.5);
		
		//炮塔歪一下
		Effect3D.Askew(this.md_tank.md_barb,true);
	}

	public function hurtEnd():void{
		ColorManager.noneColor(md_tank.getDO());
		
		//炮塔正常
		Effect3D.Askew(this.md_tank.md_barb,false);
		
	}
	
	public function reloadStart():void{
		tagPane.startReload(vo.body.ReloadShotMS);
	}

	public function reloadEnd():void{
		tagPane.endReload();
	}
	//----------------------内部动作----------------------
	//轮子旋转
	private function wheelsMove():void{
		//if(vo.hardVO.v3d_v.x!=0||vo.hardVO.v3d_v.z!=0){//转动轮子
		if(vo.bMove){
			var rotSpeed:int=((vo as MyTankVO).body.MOV/md_tank.md_wheelA.R)* MathDG.RTD;
			md_tank.md_wheelA.rotX+=rotSpeed;
			md_tank.md_wheelA.rotX%=360;
			
			md_tank.md_wheelB.rotX+=rotSpeed;
			md_tank.md_wheelB.rotX%=360;
			
			md_tank.md_wheelC.rotX+=rotSpeed;
			md_tank.md_wheelC.rotX%=360;
			
			//烟雾特效
			dusts();
			
		}else{//静止轮子
			
		}
	}
	
	//烟雾效果
	private var dustTick:int;
	
	//关于轮子的定位
	private var leftOfs:Vector3D;//左后轮子相对于整个坦克的偏移量
	private var rightOfs:Vector3D;//右后轮子相对于整个坦克的偏移量
	private var Mr:Matrix3D;
	
	private var left:Vector3D;//左后轮子相对于整个坦克的偏移量
	private var right:Vector3D;//右后轮子相对于整个坦克的偏移量
	
	private function initDusts():void{
		leftOfs=new Vector3D(-18,0,-27);//左后轮子相对于整个坦克的偏移量
		rightOfs=new Vector3D(18,0,-27);//右后轮子相对于整个坦克的偏移量
		left=new Vector3D();
		right=new Vector3D();
		Mr=new Matrix3D();
	}
	
	private function dusts():void{
		dustTick++;
		if(dustTick>1){
			dustTick=0;
			Mr.setupRotate(0, 1, 0, vo.hardVO.v3d_r.y);
			Matrix3D.SMul_VM(leftOfs, Mr, left);
			Matrix3D.SMul_VM(rightOfs, Mr, right);
			left.add(vo.hardVO.v3d_p);
			right.add(vo.hardVO.v3d_p);
			Eff3DManager.getInstance().dust(left);
			Eff3DManager.getInstance().dust(right);
		}
	}
	
	//开炮
	public function fire():void{
		
			md_tank.md_barb.gotoAndPlay("fire");
			
			Shaker.getInstance().start(-3, 2);//屏幕震动
			
			Eff3DManager.getInstance().tankFire(vo);
		
	}
}//end of class
}